//
//  TTTabView.swift
//  WeiBo
//
//  Created by 涂永江 on 2017/6/4.
//  Copyright © 2017年 涂永江. All rights reserved.
//

import UIKit


//通过代理
protocol TTTabViewDelegate :NSObjectProtocol
{

    func chuandi()

}

class TTTabView: UITabBar {

        //定义一个闭包回调事件
        var closore:(()->())?
    
    override init(frame: CGRect) {
    
        super.init(frame: frame)
      
        setUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
   //定义代理对象
    
    weak var ttdelegate : TTTabViewDelegate?
    

   
       //懒加载设置按钮
        lazy var buttoncomp :UIButton = {
        
            let button = UIButton()
            
            //按钮添加点击事件
            button.addTarget(self, action: #selector(dianji), for: .touchUpInside)
            
            //设置背景图片
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button"), for:.normal)
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), for:.highlighted)
            
            //设置图片
            button.setImage(UIImage(named:"tabbar_compose_icon_add"), for: .normal)
            button.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), for: .highlighted)
            
            button.sizeToFit()
            
            
            return button
        }()
    
    //按钮的点击事件
  @objc private func dianji()
    {
        closore?()
//        ttdelegate?.chuandi()
    
    }
    
   //设置按钮
   @objc func setUI()
    {
    
        addSubview(buttoncomp)
        
    }
    //子控件的布局
    override func layoutSubviews() {
        super.layoutSubviews()
       
        buttoncomp.centerX = width * 0.5
        buttoncomp.centerY = height * 0.5
        
        
        let width1 = self.width / 5
        var index = 0
        
        //遍历view中的子控件
        for subview in subviews
        {
        
                //当按钮是系统类型的就布局
            if subview .isKind(of: NSClassFromString("UITabBarButton")!){
                
                subview.width = width1
                
                subview.x = CGFloat(index) * width1
            
                 index += 1
                if index == 2
                {
                index += 1
                }
            }
            
            
        
        }
        
        
    }
}





